<?php
		/**
		 * 
		 * @author cyz 2010 09 01
		 *
		 */
	    class cyz_calender	
	    {
	    	public $month;
	    	public $year;
	    	public $start_week;
	    	public $days_num;
	    	
	    	public $month_old;
	    	public $year_old;
	    	
	    	function __construct($date='')
	    	{
	    		if ($date == '')
	    		{
	    			$date = date("Y-m-d H:i:s");
	    		}
	    		
	    		$date_old = strtotime($date);
	    		$this->year_old = date('Y',$date_old);
	    		$this->month_old = date('m',$date_old);
	    		$this->init($date);
	    	}
	    	
	    	function init($date='')
	    	{
	    		$date = strtotime($date);
	    		$this->year = date('Y',$date);
	    		$this->month = date('m',$date);
	    		if (function_exists('cal_days_in_month'))
	    		{
	    			$this->days_num = cal_days_in_month(CAL_GREGORIAN, $this->month, $this->year);
	    			$this->start_week = jddayofweek ( cal_to_jd(CAL_GREGORIAN, $this->month,'01', $this->year) , 0 );
	    		}else {
	    			$this->days_num   = $this->cal_days_month($this->month , $this->year);
	    			$this->start_week = $this->get_start_week($this->month , $this->year);
	    		}
	    		
	    	}
	    	
	    	function get_month()
	    	{
	    		$date = array();
	    		for ($i=0;$i<$this->days_num;$i++)
	    		{
	    			$week = $i + $this->start_week;
	    			$key = floor($week/7);
	    			$week = $week%7;
	    			$day = $i+1;
	    			$date[$day] = array($day,$week,$key);
	    		}
	    		return $date;
	    	}
	    	
	    	function get_next_month()
	    	{
	    		$this->month = $this->month_old + 1;
	    		if ($this->month == 13)
	    		{
	    			$this->month = '01';
	    			$this->year  = $this->year_old + 1;
	    		}
	    		$this->month = str_pad($this->month,2,'0',STR_PAD_LEFT);
	    		$this->init($this->year.'-'.$this->month.'-01 00:00:00');
	    		return $this->get_month();
	    	}
	    	
	   		function get_last_month()
	    	{
	    		$this->month = $this->month_old - 1;
	    		if ($this->month == 0)
	    		{
	    			$this->month = '12';
	    			$this->year  = $this->year_old - 1;
	    		}
	    		$this->month = str_pad($this->month,2,'0',STR_PAD_LEFT);
	    		$this->init($this->year.'-'.$this->month.'-01 00:00:00');
	    		return $this->get_month();
	    	}
	    	
	     	function cal_days_month($month,$year)
	    	{
	    		$days = array(0,   31,   28,   31,   30,   31,   30,   31,   31,   30,   31,   30,   31);
	    		if(   2 == $month && 0 == ($year % 4) && (   0 != ($year % 100)   ||   0 == ($year % 400)   ) ) 
	    		{
	    			$days[2] = 29;
	    		}
	    		return $days[$month];
	    	}
	    	function get_start_week($month,$year)
	    	{
	    		$months = array(1,2);
	    		if ( in_array($month,$months)) {$month = 12 + $month; }
	    		$c = floor($year/100);
	    		$y = $year%100;
	    		$m = $month;
	    		$d = 1;
	    		$w = $y + floor($y/4) + floor($c/4) - 2*$c + floor(26*($m+1)/10) + $d - 1;
	    		$w = $w % 7;
	    		if ($w < 0)$w = $w + 7;
	    		return $w;
	    	}
	    }